//
//  CustomTitleView.swift
//  Wallpaper
//
//  Created by    on 2023/12/19.
//

import UIKit
import SnapKit
class CustomTitleView: UIView {
    private let leftButton: UIButton
    private let rightButton: UIButton
    private let underlineView: UIView
    
    override init(frame: CGRect) {
        leftButton = UIButton(type: .custom)
        leftButton.setTitle("Guide", for: .normal)
        leftButton.setTitleColor(.white, for: .selected)
        leftButton.setTitleColor(.white, for: .normal)
        leftButton.setTitleColor(UIColor(colorString: "6f6f72"), for: .normal)
        leftButton.isSelected = true
        
        rightButton = UIButton(type: .custom)
        rightButton.setTitleColor(UIColor(colorString:"6f6f72"), for: .normal)
        rightButton.setTitleColor(.white, for: .selected)
        rightButton.setTitle("For you", for: .normal)
        
        underlineView = UIView()
        underlineView.layer.cornerRadius = 1
        underlineView.backgroundColor = UIColor(colorString: "2ecc70")
        
        super.init(frame: frame)
        
        // 设置左右按钮的样式和位置
        // ...
        
        // 设置下划线的样式和位置
        // ...
        
        // 添加左右按钮和下划线到容器视图
        addSubview(leftButton)
        leftButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(0)
            make.height.equalTo(34)
        }
        addSubview(rightButton)
        rightButton.snp.makeConstraints { make in
            make.left.equalTo(leftButton.snp.right).offset(20)
            make.right.top.bottom.equalTo(0)
            make.height.equalTo(34)
        }
        
        addSubview(underlineView)
        
        underlineView.snp.makeConstraints { make in
            make.centerX.equalTo(leftButton.snp.centerX)
            make.height.equalTo(2)
            make.width.equalTo(50)
            make.bottom.equalTo(0)
        }
        
        // 添加按钮点击事件
        leftButton.addTarget(self, action: #selector(leftButtonTapped), for: .touchUpInside)
        rightButton.addTarget(self, action: #selector(rightButtonTapped), for: .touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 左按钮点击事件处理
    @objc private func leftButtonTapped() {
        guard leftButton.isSelected == false else {
            return
        }
        
        underlineView.snp.remakeConstraints { make in
            make.centerX.equalTo(leftButton.snp.centerX)
            make.height.equalTo(2)
            make.width.equalTo(50)
            make.bottom.equalTo(0)
        }
        self.leftButton.isSelected = true
        self.rightButton.isSelected = false
    }
    
    // 右按钮点击事件处理
    @objc private func rightButtonTapped() {
        guard rightButton.isSelected == false else {
            return
        }
        underlineView.snp.remakeConstraints { make in
            make.centerX.equalTo(rightButton.snp.centerX)
            make.height.equalTo(2)
            make.width.equalTo(50)
            make.bottom.equalTo(0)
        }
        self.rightButton.isSelected = true
        self.leftButton.isSelected = false
    }
}
